package edu.cebanc.spring.biblioteca.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.bean.AutorBean;
import edu.cebanc.spring.biblioteca.bean.EditorialBean;
import edu.cebanc.spring.biblioteca.bean.GeneroBean;
import edu.cebanc.spring.biblioteca.bean.IdiomaBean;
import edu.cebanc.spring.biblioteca.dao.AutoresDAO;
import edu.cebanc.spring.biblioteca.dao.EditorialesDAO;
import edu.cebanc.spring.biblioteca.dao.GenerosDAO;
import edu.cebanc.spring.biblioteca.dao.IdiomasDAO;
import edu.cebanc.spring.biblioteca.util.DataParser;

@Service("datosGeneralService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DatosGeneralServiceImpl implements DatosGeneralService {

	@Autowired
	AutoresDAO autoresDAO;
	@Autowired
	GenerosDAO generosDAO;
	@Autowired
	IdiomasDAO idiomasDAO;
	@Autowired
	EditorialesDAO editorialesDAO;
	
	@Override
	public List<AutorBean> listadoAutores() {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		return dp.fromAutorListToAutorBeanList(autoresDAO.getAutores());
	}

	@Override
	public AutorBean cargarAutor(String id) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		
		return dp.fromAutorToAutorBean(autoresDAO.getAutor(Integer.parseInt(id)));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void guardarAutor(AutorBean autor) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		
		autoresDAO.saveAutor(dp.fromAutorBeanToAutor(autor));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void eliminarAutor(String id) {
		// TODO Auto-generated method stub
		autoresDAO.deleteAutor(autoresDAO.getAutor(Integer.parseInt(id)));
	}

	@Override
	public List<GeneroBean> listadoGeneros() {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		return dp.fromGeneroListToGeneroBeanList(generosDAO.listGeneros());
	}

	@Override
	public GeneroBean cargarGenero(String id) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		return dp.fromGeneroToGeneroBean(generosDAO.getGenero(Integer.parseInt(id)));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void guardarGenero(GeneroBean genero) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		generosDAO.saveGenero(dp.fromGeneroBeanToGenero(genero));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void eliminarGenero(String id) {
		// TODO Auto-generated method stub
		generosDAO.deleteGenero(generosDAO.getGenero(Integer.parseInt(id)));
	}

	@Override
	public List<IdiomaBean> listadoIdiomas() {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		return dp.fromIdiomaListToIdiomaBeanList(idiomasDAO.listIdiomas());
	}

	@Override
	public IdiomaBean cargarIdioma(String id) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		return dp.fromIdiomaToIdiomaBean(idiomasDAO.getIdioma(Integer.parseInt(id)));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void guardarIdioma(IdiomaBean idioma) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		idiomasDAO.saveIdioma(dp.fromIdiomaBeanToIdioma(idioma));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void eliminarIdioma(String id) {
		// TODO Auto-generated method stub
		idiomasDAO.deleteIdioma(idiomasDAO.getIdioma(Integer.parseInt(id)));
	}

	@Override
	public List<EditorialBean> listadoEditoriales() {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		
		return dp.fromEditorialListToEditorialBeanList(editorialesDAO.listEditoriales());
	}

	@Override
	public EditorialBean cargarEditorial(String id) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		return dp.fromEditorialToEditorialBean(editorialesDAO.getEditorial(Integer.parseInt(id)));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void guardarEditorial(EditorialBean editorial) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();
		editorialesDAO.saveEditorial(dp.fromEditorialBeanToEditorial(editorial));
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void eliminarEditorial(String id) {
		// TODO Auto-generated method stub
		editorialesDAO.deleteEditorial(editorialesDAO.getEditorial(Integer.parseInt(id)));
	}

}
